package modules.lobby.view.tableListUI
{
	/**
	 *
	 * @author warden_feng
	 * 2013-5-6
	 */
	import com.feng.components.FList;

	import flash.display.MovieClip;
	import flash.events.Event;

	import communication.hall.HallMsgSender;

	import modules.GameEvent;
	import modules.ViewManager;
	import modules.lobby.LobbyEvent;
	import modules.lobby.data.LobbyDataModel;
	import modules.lobby.data.LobbyState;
	import modules.lobby.data.TournamentModel;

	public class TableListUIManager extends ViewManager
	{
		private var tableList:FList;

		/** 锦标赛过滤条件 */
		private var tournamentFilter:Object;

		public static var _showState:String = TableListItem.CASHTABLELIST_STATE;

		private function set showState(value:String):void
		{
			_showState = value;

			mainUI.normalTableTitle.visible = false;
			mainUI.tournamentTableTitle.visible = false;

			switch (_showState)
			{
				case TableListItem.FREETABLELIST_STATE:
					mainUI.normalTableTitle.visible = true;
					break;
				case TableListItem.CASHTABLELIST_STATE:
					mainUI.normalTableTitle.visible = true;
					break;
				case TableListItem.TOURNAMENTLIST_STATE:
					mainUI.tournamentTableTitle.visible = true;
					break;
			}
		}

		private function get showState():String
		{
			return _showState;
		}

		public function TableListUIManager(mainUI:MovieClip)
		{
			this.mainUI = mainUI;

			init();
		}

		override protected function init():void
		{
			tableList = FList.getInstance(mainUI.tableList);
			tableList.listItemClass = TableListItem;
			tableList.alternateRows = true;
			tableList.autoHideScrollBar = true;

			//请求桌子列表
			HallMsgSender.instance.sendTableList();

			mainUI.addEventListener(Event.ADDED_TO_STAGE, onAddToStage);
			mainUI.removeEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
		}

		override protected function onAddToStage(event:Event):void
		{
			dispatcher.addEventListener(LobbyEvent.REFRESH_TABLE_LIST_UI, onReFresh);

			dispatcher.addEventListener(LobbyEvent.SHOW_FREETABLE_LIST, onShowFreetableList);

			dispatcher.addEventListener(LobbyEvent.SHOW_NORMALTABLE_LIST, onShowNormaltableList);

			dispatcher.addEventListener(LobbyEvent.SHOW_TOURNAMENT_LIST, onShowTournamentList);

			//添加现金桌子刷新
			dispatcher.dispatchEvent(new GameEvent(GameEvent.ADD_CYCLE_FUNCTION, {name: "lobbyTableListReq", preTime: 0, run: function():void
			{
				//请求桌子列表
				HallMsgSender.instance.sendTableList();
			}, need: function():Boolean
			{
				return LobbyState.needUpdateTableList;
			}, cycle: 3000}));
		}

		override protected function onRemovedFromStage(event:Event):void
		{
			dispatcher.removeEventListener(LobbyEvent.REFRESH_TABLE_LIST_UI, onReFresh);

			dispatcher.removeEventListener(LobbyEvent.SHOW_FREETABLE_LIST, onShowFreetableList);

			dispatcher.removeEventListener(LobbyEvent.SHOW_NORMALTABLE_LIST, onShowNormaltableList);

			dispatcher.removeEventListener(LobbyEvent.SHOW_TOURNAMENT_LIST, onShowTournamentList);

		}

		override protected function updateView():void
		{
			// TODO Auto Generated method stub
			super.updateView();
		}

		/**
		 * 刷新界面数据
		 **/
		private function onReFresh(event:LobbyEvent):void
		{
			reFresh();
		}

		/**
		 * 显示免费桌子数据
		 **/
		private function onShowFreetableList(event:LobbyEvent):void
		{
			reFresh();
		}

		/**
		 * 显示现金桌子数据
		 **/
		private function onShowNormaltableList(event:LobbyEvent):void
		{
			reFresh();
		}

		/**
		 * 显示锦标赛列表
		 **/
		private function onShowTournamentList(event:LobbyEvent = null):void
		{
			if (event && event.data)
			{
				tournamentFilter = event.data;
			}

			reFresh()
		}

		private function reFresh():void
		{
			switch (showState)
			{
				case TableListItem.FREETABLELIST_STATE:
					updateFreeTableData();
					break;
				case TableListItem.CASHTABLELIST_STATE:
					updateCashTableData();
					break;
				case TableListItem.TOURNAMENTLIST_STATE:
					showTournamentList();
					break;
			}
		}

		/**
		 *  显示免费桌子数据
		 **/
		private function updateFreeTableData():void
		{

		}

		/**
		 *  显示现金桌子数据
		 **/
		private function updateCashTableData():void
		{
			tableList.items = LobbyDataModel.cashTableShowData;
		}

		/**
		 *  显示锦标赛数据
		 **/
		private function showTournamentList():void
		{
			var tournamentList:Array = getShowTurnamentList();
			tableList.items = tournamentList;
		}

		/** 通过过滤后的列表 */
		private function getShowTurnamentList():Array
		{
			var tournamentList:Array = LobbyDataModel.tournamentData.filter(function(element:TournamentModel, index:int, arr:Array):Boolean
			{
				if (tournamentFilter && tournamentFilter.hasOwnProperty("isSitAndGo"))
				{
					if (tournamentFilter.isSitAndGo)
					{
						return element.type == 2;
					}
					return element.type != 2;
				}
				return true;
			});
			return tournamentList;
		}

	}
}
